package com.simin.base.apputils;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.ColorInt;


import com.simin.base.R;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * date:2023/11/14
 * author:wsm
 * faucation:状态栏主题色相关
 */
public class EasyStatusBarUtil {

    //半透明矩形条的默认ALPHA
    public static final int STATUS_BAR_VIEW_ID = R.id.status_bar_view;
    public static final int TRANSLUCENT_VIEW_ID = R.id.translucent_bar_view;

    private ThemeType statusBarType;
    private ThemeType navigationBarType;

    private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
    private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";

    /**
     * 状态栏颜色
     */
    private int statusBarColor;
    /**
     * 底部导航栏颜色
     */
    private int navigationBarColor;
    /**
     * 状态栏是否添加半透明蒙版，可加深状态栏的颜色
     */
    private boolean translucentEnable;
    /**
     * 底部导航栏是否需要透明
     */
    private boolean navigationBarTransparentEnable;
    /**
     * @see #apply(Activity) 方法是否是在某个可以被切换的Fragment里被使用？
     */
    private boolean isAppliedInTabFragment;
    /**
     * 半透明蒙版的alpha
     */
    private int translucentAlpha;
    /**
     * 状态栏可被填充时，可设置需要MarginTop的View，防止被填充到状态栏
     */
    private View needOffsetView;
    /**
     * 不需要设置状态栏的Activity的class，在全局设置时过滤这些class
     *
     * @see #applyAllPage(Application)
     */
    private List<String> filterActivityClass;


    private EasyStatusBarUtil(Builder statusBarBuilder) {
        this.statusBarColor = statusBarBuilder.statusBarColor;
        this.navigationBarColor = statusBarBuilder.navigationBarColor;
        this.translucentEnable = statusBarBuilder.translucentEnable;
        this.navigationBarTransparentEnable = statusBarBuilder.navigationBarTransparentEnable;
        this.translucentAlpha = statusBarBuilder.translucentAlpha;
        this.needOffsetView = statusBarBuilder.needOffsetView;
        this.statusBarType = statusBarBuilder.statusBarType;
        this.navigationBarType = statusBarBuilder.navigationBarType;
        this.filterActivityClass = statusBarBuilder.filterActivityClass;
        this.isAppliedInTabFragment = statusBarBuilder.isAppliedInTabFragment;
    }

    /**
     * 唯一一个对外暴露的方法,用于对状态栏和底部导航栏UI的改变
     *
     * @param activity
     */
    public void apply(Activity activity) {
        applyStatusBarType(activity);
        applyNavigationBarType(activity);

    }

    private void applyStatusBarType(Activity activity) {
        if (activity == null) {
            return;
        }
        switch (statusBarType) {
            case SET_COLOR:
                applyStatusBarColor(activity);
                break;
            case SET_TRANSPARENT:
                applyStatusBarTransparent(activity);
                break;
            case NULL:
                break;
        }
    }

    private void applyNavigationBarType(Activity activity) {
        if (activity == null) {
            return;
        }
        switch (navigationBarType) {
            case SET_COLOR:
                applyNavigationBarColor(activity);
                break;
            case SET_TRANSPARENT:
                applyNavigationBarTransparent(activity);
                break;
            case NULL:
                break;
        }
    }

    /**
     * 对app所有页面改变状态栏主题颜色,请在Application的onCreate方法中使用
     *
     * @param application
     */
    public void applyAllPage(Application application) {
        application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }


            @Override
            public void onActivityStarted(Activity activity) {
                if (activity == null) {
                    return;
                }
                if (filterActivityClass == null || filterActivityClass.size() == 0
                        || (!filterActivityClass.contains(activity.getClass().getName()))) {
                    apply(activity);
                }
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }

    /**
     * 修改状态栏颜色
     *
     * @param activity 该页面的Activity
     */
    private void applyStatusBarColor(Activity activity) {
        if (activity == null || activity.getWindow() == null) {
            return;
        }
        if (!translucentEnable) {
            translucentAlpha = 0;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //SDK>5.0 直接着色
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            activity.getWindow().setStatusBarColor(calculateStatusColor(statusBarColor, translucentAlpha));
            if (isAppliedInTabFragment || navigationBarTransparentEnable) {
                setFitsSystemWindowsInFragment(activity);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //SDK>4.4需add一个着色的view到状态栏
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            View statusBarView = decorView.findViewById(STATUS_BAR_VIEW_ID);
            if (statusBarView != null) {
                if (statusBarView.getVisibility() == View.GONE) {
                    statusBarView.setVisibility(View.VISIBLE);
                }
                statusBarView.setBackgroundColor(calculateStatusColor(statusBarColor, translucentAlpha));
            } else {
                decorView.addView(createStatusBarView(activity, statusBarColor, translucentAlpha));
            }
            if (isAppliedInTabFragment) {
                setFitsSystemWindowsInFragment(activity);
            } else {
                setFitsSystemWindowsInActivity(activity);//自适应状态栏padding
            }
        }

        justMarginStatusBarHeight(activity, needOffsetView);
    }

    private void applyNavigationBarColor(Activity activity) {
        if (activity == null || activity.getWindow() == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().setNavigationBarColor(navigationBarColor);
        }

    }

    /**
     * 设置状态栏透明且空间可被填充
     *
     * @param activity 设置页面的Activity
     */
    private void applyStatusBarTransparent(Activity activity) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return;
        }
        if (!translucentEnable) {
            translucentAlpha = 0;
        }
        //设置状态栏透明且空间可被填充
        setTransparentForWindow(activity);
        //@see #apply(Activity) 方法是否是在某个可以被切换的Fragment里被使用？
        if (isAppliedInTabFragment) {
            //清除之前的Fragment状态栏设置时有可能添加的着色View
            clearStatusBarView(activity);
            //清除之前的Fragment设置的PaddingTop状态栏高度，避免状态栏不可被填充
            clearFitsSystemWindowsInFragment(activity);
        }

        //添加半透明蒙版View（如果不需要 alpha=0）
        addTranslucentView(activity, translucentAlpha);
        //needOffsetView如果不为空，偏移该View防止填充到状态栏
        marginStatusBarHeight(activity, needOffsetView);
    }

    private void applyNavigationBarTransparent(Activity activity) {
        if (activity == null || activity.getWindow() == null) {
            return;
        }
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            return;
        }
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

    /**
     * 4.4<=sdk<5.0时状态栏变色是通过填充View，因此需要清除
     * 如果设置状态栏透明可用，则需要调用该方法去除之前填充的View
     *
     * @param activity
     */
    private void clearStatusBarView(Activity activity) {
        if (activity == null || activity.getWindow() == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {

            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            View statusBarView = decorView.findViewById(STATUS_BAR_VIEW_ID);
            if (statusBarView != null) {
                decorView.removeView(statusBarView);
            }
        }

    }

    /**
     * 添加alpha透明的黑色矩形条
     *
     * @param activity       需要设置的 activity
     * @param statusBarAlpha 透明值
     */
    private void addTranslucentView(Activity activity, int statusBarAlpha) {
        if (activity == null) {
            return;
        }
        ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
        View fakeTranslucentView = contentView.findViewById(TRANSLUCENT_VIEW_ID);
        if (fakeTranslucentView != null) {
            if (fakeTranslucentView.getVisibility() == View.GONE) {
                fakeTranslucentView.setVisibility(View.VISIBLE);
            }
            fakeTranslucentView.setBackgroundColor(Color.argb(statusBarAlpha, 0, 0, 0));
        } else {
            contentView.addView(createTranslucentStatusBarView(activity, statusBarAlpha));
        }
    }

    /**
     * @see #apply(Activity) 该方法在Activity里被调用时，用于设置根布局参数
     * 使其Activity布局自适应状态栏，如果状态栏可被填充，设置该属性的Activity布局自动padding状态栏的高度
     * 目前SDK<19的setColor时状态栏可以被填充，因此需要使用该方法。
     */
    private static void setFitsSystemWindowsInActivity(Activity activity) {
        ViewGroup parent = (ViewGroup) activity.findViewById(android.R.id.content);
        if (parent == null) {
            return;
        }
        for (int i = 0, count = parent.getChildCount(); i < count; i++) {
            View childView = parent.getChildAt(i);
            if (childView == null) {
                continue;
            }
            if (childView instanceof ViewGroup) {
                //自适应状态栏的高度，给外层viewGroup添加Padding状态栏的高度
                childView.setFitsSystemWindows(true);
                //如果有滑动的话，设置clipToPadding=true，ListView、RecyclerView不会滑到状态栏上面去
                ((ViewGroup) childView).setClipToPadding(true);
            }
        }
    }


    /**
     * @see #apply(Activity) 该方法在Fragment里被调用时，用于设置根布局参数
     * 使Fragment所在的Activity的布局自适应状态栏，如果状态栏可被填充，设置该属性的Activity布局自动padding状态栏的高度
     * 目前SDK<19的setColor时状态栏可以被填充，因此需要使用该方法。
     */
    private static void setFitsSystemWindowsInFragment(Activity activity) {
        ViewGroup parent = (ViewGroup) activity.findViewById(android.R.id.content);
        if (parent == null) {
            return;
        }
        for (int i = 0, count = parent.getChildCount(); i < count; i++) {
            View childView = parent.getChildAt(i);
            if (childView == null) {
                continue;
            }
            if (childView instanceof ViewGroup) {
                //自适应状态栏的高度，给外层viewGroup添加Padding状态栏的高度
                //  childView.setFitsSystemWindows(true);
                childView.setPadding(childView.getPaddingLeft(), getStatusBarHeight(activity)
                        , childView.getPaddingRight(), childView.getPaddingBottom());
                //如果有滑动的话，设置clipToPadding=true，ListView、RecyclerView不会滑到状态栏上面去
                ((ViewGroup) childView).setClipToPadding(true);
                //   childView.requestLayout();
            }
        }
    }

    /**
     * @param activity
     * @see #setFitsSystemWindowsInFragment(Activity) 清除该方法在Fragment里被调用时
     * 使得Activity布局PaddingTop状态栏高度
     */
    private static void clearFitsSystemWindowsInFragment(Activity activity) {
        ViewGroup parent = (ViewGroup) activity.findViewById(android.R.id.content);
        if (parent == null) {
            return;
        }
        for (int i = 0, count = parent.getChildCount(); i < count; i++) {
            View childView = parent.getChildAt(i);
            if (childView == null) {
                continue;
            }
            if (childView instanceof ViewGroup) {
                //自适应状态栏的高度，给外层viewGroup添加Padding状态栏的高度
                //  childView.setFitsSystemWindows(true);
                childView.setPadding(0, 0, 0, 0);
                //如果有滑动的话，设置clipToPadding=true，ListView、RecyclerView不会滑到状态栏上面去
                //  ((ViewGroup) childView).setClipToPadding(true);
            }
        }
    }

    /**
     * 设置状态栏透明且可被布局填充
     *
     * @param activity 设置页面的activity
     */
    private static void setTransparentForWindow(Activity activity) {
        if (activity == null || activity.getWindow() == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //状态栏着色成透明
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
            if (activity.getWindow().getDecorView() != null) {
                //设置状态栏空间可被布局填充
                activity.getWindow()
                        .getDecorView()
                        .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            }

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //状态栏透明且空间可被填充
            activity.getWindow()
                    .addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    /**
     * 创建半透明矩形条 View
     *
     * @param alpha 透明值
     * @return 半透明 View
     */
    private View createTranslucentStatusBarView(Activity activity, int alpha) {
        // 绘制一个和状态栏一样高的矩形
        View statusBarView = new View(activity);
        LinearLayout.LayoutParams params =
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
        statusBarView.setLayoutParams(params);
        statusBarView.setBackgroundColor(Color.argb(alpha, 0, 0, 0));
        statusBarView.setId(TRANSLUCENT_VIEW_ID);
        return statusBarView;
    }

    /**
     * 生成颜色矩形条 view
     *
     * @param activity 需要设置的activity
     * @param color    状态栏颜色值
     * @param alpha    透明值
     * @return 状态栏矩形条
     */
    private View createStatusBarView(Activity activity, @ColorInt int color, int alpha) {
        // 绘制一个和状态栏一样高的矩形
        View statusBarView = new View(activity);
        LinearLayout.LayoutParams params =
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
        statusBarView.setLayoutParams(params);
        statusBarView.setBackgroundColor(calculateStatusColor(color, alpha));
        statusBarView.setId(STATUS_BAR_VIEW_ID);
        return statusBarView;
    }

    /**
     * 获取状态栏高度
     *
     * @param context context
     * @return 状态栏高度
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        // 获得状态栏高度
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        } else {
            try {
                Class<?> clazz = Class.forName("com.android.internal.R$dimen");
                Object object = clazz.newInstance();
                int height = Integer.parseInt(clazz.getField("status_bar_height")
                        .get(object).toString());
                result = context.getResources().getDimensionPixelSize(height);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }


    /**
     * 获取底部导航栏高度
     *
     * @return
     */
    public static int getNavigationBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        //获取NavigationBar的高度
        return resources.getDimensionPixelSize(resourceId);
    }

    public static void clearStatusBarFlags(Activity activity) {
        if (activity == null || activity.getWindow() == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    /**
     * 计算状态栏颜色
     *
     * @param color color值
     * @param alpha alpha值
     * @return 最终的状态栏颜色
     */
    private int calculateStatusColor(@ColorInt int color, int alpha) {
        if (alpha == 0) {
            return color;
        }
        float a = 1 - alpha / 255f;
        int red = color >> 16 & 0xff;
        int green = color >> 8 & 0xff;
        int blue = color & 0xff;
        red = (int) (red * a + 0.5);
        green = (int) (green * a + 0.5);
        blue = (int) (blue * a + 0.5);
        return 0xff << 24 | red << 16 | green << 8 | blue;
    }

    /**
     * 需要手动margin的控件，防止控件填充到状态栏
     * layoutParams.topMargin += getStatusBarHeight(activity)
     *
     * @param activity
     * @param needOffsetView
     */
    private void marginStatusBarHeight(Activity activity, View needOffsetView) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT
                || activity == null || needOffsetView == null) {
            return;
        }
        if (needOffsetView != null && needOffsetView.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) needOffsetView.getLayoutParams();
            layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin + getStatusBarHeight(activity),
                    layoutParams.rightMargin, layoutParams.bottomMargin);
        }
    }

    /**
     * 需要手动margin的控件，防止控件填充到状态栏
     * layoutParams.topMargin == getStatusBarHeight(activity)
     *
     * @param activity
     * @param needOffsetView
     */
    private void justMarginStatusBarHeight(Activity activity, View needOffsetView) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT
                || activity == null || needOffsetView == null) {
            return;
        }
        if (needOffsetView.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) needOffsetView.getLayoutParams();
            layoutParams.setMargins(layoutParams.leftMargin, getStatusBarHeight(activity),
                    layoutParams.rightMargin, layoutParams.bottomMargin);
        }
    }

    public static class Builder {

        private static final int DEFAULT_TRANSLUCENT_ALPHA = 56;
        private int statusBarColor = Color.parseColor("#f9f9f9");
        private int navigationBarColor;
        private boolean translucentEnable = true;
        private boolean navigationBarTransparentEnable = false;
        private boolean isAppliedInTabFragment = false;
        private int translucentAlpha = DEFAULT_TRANSLUCENT_ALPHA;
        private View needOffsetView;
        private List<String> filterActivityClass = new ArrayList<>();
        private ThemeType statusBarType = ThemeType.SET_COLOR;
        private ThemeType navigationBarType = ThemeType.NULL;

        /**
         * 设置状态栏颜色
         *
         * @param statusBarColor 颜色代码
         * @return
         */
        public Builder setStatusBarColor(int statusBarColor) {
            this.statusBarColor = statusBarColor;
            statusBarType = ThemeType.SET_COLOR;
            return this;
        }

        /**
         * 设置底部导航栏颜色
         *
         * @param navigationBarColor 颜色代码
         */
        public Builder setNavigationBarColor(int navigationBarColor) {
            this.navigationBarColor = navigationBarColor;
            navigationBarType = ThemeType.SET_COLOR;
            navigationBarTransparentEnable = false;
            return this;
        }

        /**
         * 设置状态栏透明且可被布局填充
         */
        public Builder setStatusBarTransparent() {
            statusBarType = ThemeType.SET_TRANSPARENT;
            return this;
        }

        /**
         * 设置底部导航栏可用且可被布局填充
         */
        public Builder setNavigationBarTransparent() {
            navigationBarType = ThemeType.SET_TRANSPARENT;
            navigationBarTransparentEnable = true;
            return this;
        }

        /**
         * 设置状态栏是否添加半透明蒙版，可加深颜色
         *
         * @param translucentEnable 是否添加半透明蒙版
         */
        public Builder setTranslucentEnable(boolean translucentEnable) {
            this.translucentEnable = translucentEnable;
            return this;
        }

        /**
         * @param isAppliedInTabFragment true or false
         * @see #apply(Activity) 方法是否是在某个可以被切换的Fragment里被使用？
         */
        public Builder setAppliedInTabFragment(boolean isAppliedInTabFragment) {
            this.isAppliedInTabFragment = isAppliedInTabFragment;
            return this;
        }

        /**
         * 设置半透明蒙版的透明度alpha
         *
         * @param translucentAlpha 透明度0-255
         */
        public Builder setTranslucentAlpha(int translucentAlpha) {
            this.translucentAlpha = translucentAlpha;
            return this;
        }

        /**
         * 状态栏可被填充时，可设置需要MarginTop的View，防止被填充到状态栏
         *
         * @param needOffsetView 需要MarginTop状态栏的View
         */
        public Builder setNeedOffsetView(View needOffsetView) {
            this.needOffsetView = needOffsetView;
            return this;
        }

        /**
         * 添加不需要设置状态栏的Activity集合，当全局设置的时候
         *
         * @param activityClass Activity的class.getName
         */
        public Builder addFilterPage(Class<? extends Activity> activityClass) {
            if (activityClass == null) {
                return this;
            }
            filterActivityClass.add(activityClass.getName());

            return this;
        }

        /**
         * 添加不需要设置状态栏的Activity Class集合，当全局设置的时候
         *
         * @param activityClasses Activity的class.getName的集合
         */
        public Builder addFilterPage(List<Class<? extends Activity>> activityClasses) {
            for (Class<? extends Activity> temp : activityClasses) {
                filterActivityClass.add(temp.getName());
            }

            return this;
        }

        /**
         * 获取默认Builder属性
         *
         * @return
         */
        public Builder getDefault() {
            return this;
        }

        /**
         * 根据Builder创建StatusBarUtil工具类
         *
         * @return
         */
        public EasyStatusBarUtil build() {
            return new EasyStatusBarUtil(this);
        }
    }

    /**
     * 设置状态栏文字色值为深色调
     *
     * @param useDart  是否使用深色调
     * @param activity
     */
    public static void setStatusTextColor(boolean useDart, Activity activity) {
        if (isFlyme()) {
            processFlyMe(useDart, activity);
        } else if (isMIUI()) {
            processMIUI(useDart, activity);
        } else {
            if (useDart) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                }
            } else {
                activity.getWindow().getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            }
        }
    }

    /**
     * 判断手机是否是魅族
     *
     * @return
     */
    public static boolean isFlyme() {
        try {
            // Invoke Build.hasSmartBar()
            final Method method = Build.class.getMethod("hasSmartBar");
            return method != null;
        } catch (final Exception e) {
            return false;
        }
    }

    /**
     * 判断手机是否是小米
     *
     * @return
     */
    public static boolean isMIUI() {
        try {
            final BuildProperties prop = BuildProperties.newInstance();
            return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
                    || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
                    || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;
        } catch (final IOException e) {
            return false;
        }
    }

    /**
     * 改变魅族的状态栏字体为黑色，要求FlyMe4以上
     */
    private static void processFlyMe(boolean isLightStatusBar, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        try {
            Class<?> instance = Class.forName("android.view.WindowManager$LayoutParams");
            int value = instance.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON").getInt(lp);
            Field field = instance.getDeclaredField("meizuFlags");
            field.setAccessible(true);
            int origin = field.getInt(lp);
            if (isLightStatusBar) {
                field.set(lp, origin | value);
            } else {
                field.set(lp, (~value) & origin);
            }
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }
    }

    /**
     * 改变小米的状态栏字体颜色为黑色, 要求MIUI6以上  lightStatusBar为真时表示黑色字体
     */
    private static void processMIUI(boolean lightStatusBar, Activity activity) {
        Class<? extends Window> clazz = activity.getWindow().getClass();
        try {
            int darkModeFlag;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(activity.getWindow(), lightStatusBar ? darkModeFlag : 0, darkModeFlag);
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }
    }

    public enum ThemeType {
        SET_COLOR,
        SET_TRANSPARENT,
        NULL
    }

}
